package com.woniu.user.controller;


import com.woniu.entity.Result;
import com.woniu.user.pojo.PickAddress;
import com.woniu.user.service.IPickAddressService;
import com.woniu.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/pick-address")
@Api(tags = "用户地址管理")
public class PickAddressController {

    @Autowired
    private IPickAddressService pickAddressService;

    @GetMapping("/addressInfo/{id}")
    @ApiOperation("根据收货id查询该条地址数据")
    public Result<PickAddress> queryById(@PathVariable String id){
        PickAddress pickAddress = pickAddressService.getById(id);
        return Result.ok(pickAddress);
    }

    @GetMapping("/addressInfo/")
    @ApiOperation("根据用户名查询名下存储的所有地址")
    public Result<List<PickAddress>> queryAll(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtil.parseJWT(token);
        String username = claims.get("username").toString();
        List<PickAddress> pickAddressList = pickAddressService.queryAll(username);
        return Result.ok(pickAddressList);
    }
}
